﻿using CQIE.LabAssist.DataAccesss;
using CQIE.LabAssist.Models;
using CQIE.LabAssist.PublicHelp;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CQIE.LabAssist.Controllers
{
    /// <summary>
    /// 学期、学院管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MangmentController : Controller
    {
        private readonly IMangmentService _mangmentService;

        public MangmentController(_DbContext _DbContext, IMangmentService mangmentService)
        {
            _mangmentService = mangmentService;
        }

        #region 学期管理
        #region 获取所有学期
        /// <summary>
        /// 获取所有学期
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getAllSemester()
        {
            return new JsonResult(_mangmentService.getAllSemester());
        }
        #endregion

        #region 获取当前学期
        /// <summary>
        /// 获取当前学期
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getCurrentSemester()
        {
            return new JsonResult(_mangmentService.getCurrentSemester());
        }
        #endregion

        #region 新增学期

        /// <summary>
        /// 新增学期
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult addSemester(string name, bool state)
        {
          
            if (_mangmentService.addSemester(name, state))
            {
                return new JsonResult("添加成功");
            }
            else
            {
                return new JsonResult("添加失败");

            }

        }
        #endregion

        #region 删除学期

        /// <summary>
        /// 删除学期
        /// </summary>
        /// <param name="semesterId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult deleteSemester(int semesterId)
        {
            if (_mangmentService.deleteSemester(semesterId))
            {
                return new JsonResult("删除成功");
            }
            else
            {
                return new JsonResult("删除失败");

            }

        }
        #endregion

        #region 修改学期
        public record se
        {
            public int Id { get; set; }

            /// <summary>
            /// 学院名称
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// 学院状态
            /// </summary>
            public bool State { get; set; }

        }
        /// <summary>
        /// 修改学期
        /// </summary>
        /// <param name="semester"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult updateSemester([FromBody] se semester)
        {
            Semester se = new Semester();
            se.Id = semester.Id;
            se.Name=semester.Name;
            se.State=semester.State;
            if (_mangmentService.updateSemester(se))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return new JsonResult("修改失败");
            }
        }
        #endregion
        #endregion
       

        #region 学院管理
        #region 获取所有学院
        /// <summary>
        /// 获取所有学院
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public  JsonResult getAllAcademician()
        {
            return new JsonResult(_mangmentService.getAllAcademician());
        }
        #endregion

        #region 新增学院

        /// <summary>
        /// 新增学院
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult addAcademician(string name,bool state)
        {

            if (_mangmentService.addAcademician(name,state))
            {
                return new JsonResult("添加成功");
            }
            else
            {
                return new JsonResult("添加失败");

            }

        }
        #endregion

        #region 删除学院

        /// <summary>
        /// 删除学院
        /// </summary>
        /// <param name="academicianId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult deleteAcademician(int academicianId)
        {
            if (_mangmentService.deleteAcademician(academicianId))
            {
                return new JsonResult("删除成功");
            }
            else
            {
                return new JsonResult("删除失败");

            }

        }
        #endregion

        #region 修改学院
        /// <summary>
        /// 修改学院
        /// </summary>
        /// <param name="academician"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult updateAcademician([FromBody] Academician  academician)
        {
            if (_mangmentService.updateAcademician(academician))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return new JsonResult("修改失败");
            }
        }
        #endregion
        #endregion


        #region 导出安全检查记录
        /// <summary>
        /// 导出安全检查记录
        /// </summary>
        /// <param name="pubCheckList"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult exportExcel([FromBody]List<PubCheck> pubCheckList)
        {
            string fileName = _mangmentService
                .exportExcel(pubCheckList, "安全检查记录" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx");

            return new JsonResult("https://localhost:7166/"+fileName);

        }
        #endregion

    }
}
